define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'withdraw/index',
                    add_url: 'withdraw/add',
                    edit_url: 'withdraw/edit',
                    del_url: 'withdraw/del',
                    multi_url: 'withdraw/multi',
                    table: 'withdraw',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search:false,
                searchFormVisible:true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'merid', title: __('Merid')},
                        {field: 'user.username', title: __('User_id'),operate:'LIKE %...%'},
                        {field: 'user.mobile', title: __('手机'),operate:'LIKE %...%'},
                        {field: 'user_verified.true_name', title: __('真实姓名'),operate:'LIKE %...%'},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'amount', title: __('Amount'), operate:'BETWEEN'},
                        {field: 'fees', title: __('Fees'), operate:false},
                        {field: 'real_amount', title: __('Real_amount'), operate:false},
                        // {field: 'update_time', title: __('Update_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'account_type', title: __('Account_type')},
                        {field: 'account_no', title: __('Account_no')},
                        {field: 'account_name', title: __('Account_name')},
                        {field: 'status', title: __('Status'), visible:false, searchList: {"1":'待处理','2':'已转帐','3':'已退款'}},
                        {field: 'status_text', title: __('Status'), operate:false},
                        {field: 'handle_time', title: __('Handle_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'transfer_no', title: __('Transfer_no')},
                        {field: 'remark', title: __('Remark'), operate:false},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            buttons: [
                                {name: 'deny', text: '退款', title: '退款', icon: '', classname: 'btn btn-xs btn-warning btn-ajax', url: 'withdraw/deny',confirm:'确定退款回用户帐户吗？', success:function(){table.bootstrapTable('refresh', {});}},
                                {name: 'pass', text: '转帐成功', title: '转帐成功', icon: '', classname: 'btn btn-xs btn-danger btn-dialog', url: 'withdraw/edit'},
                            ],
                            events: Table.api.events.operate,
                            formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                if(row.status != 0) {
                                    $(table).data("operate-deny", null);
                                    $(table).data("operate-pass", null);
                                }
                                $(table).data("operate-del", null);
                                $(table).data("operate-edit", null);
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        pass: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});